/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.spielo.crystaldesigner.dao;

import com.spielo.crystaldesigner.property.PropertyHolder;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

/**
 *
 * @author Bernhard
 */
public class PropertyReader extends AbstractFilereader{
    
    private DocumentBuilderFactory documentBuilderFactory;
    private DocumentBuilder documentBuilder;
    private Document document;
    private PropertyHolder properties = PropertyHolder.getInstance();
    
    public PropertyReader() 
    {
        try {
            documentBuilderFactory = DocumentBuilderFactory.newInstance();
            documentBuilder = documentBuilderFactory.newDocumentBuilder();
            String fileName = (String) properties.getProperty("path");
            fileName += File.separator + "packageinfo.xml";
            document = documentBuilder.parse(new File(fileName));
        } catch (SAXException | IOException | ParserConfigurationException ex) {
            Logger.getLogger(PropertyReader.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void readProperties()
    {
        HashMap<String,String> fileProperties = new HashMap<>();
        Node packageInfo = document.getElementsByTagName("PackageInfo").item(0);
        
        NamedNodeMap attributes = packageInfo.getAttributes();
        for(int i = 0; i < attributes.getLength(); i++)
        {
            
            Node attribute = attributes.item(i);
            String key = attribute.getNodeName();
            String value = attribute.getNodeValue();
            fileProperties.put(key, value);
        }

        properties.addProperty("packageProperties", fileProperties);
    }
    
    public static void main(String[] args) {
        PropertyReader pr;
 
            pr = new PropertyReader();
            pr.readProperties();

       
                
    }
    
}